package net.xju.dao;

import com.alibaba.fastjson2.util.DateUtils;
import net.xju.common.utils.JdbcUtilsV1;
import net.xju.common.utils.PageInfo;
import net.xju.entity.Goods;
import net.xju.entity.Vo.GoodsVo;

import java.util.ArrayList;
import java.util.List;

public class GoodsDao {

    // 分页查询
    public PageInfo<GoodsVo> pageList(Goods goods, int pageNum, int pageSize) {

        String sql = "SELECT g.id,g.`name`,g.intro,g.flag,g.picture,g.price,g.pubdate,g.type_id typeId," +
                "g.create_time createTime,gt.`name` typeName FROM goods g LEFT JOIN goodstype gt ON g.type_id = gt.id WHERE g.deleted = 0";
        List<Object> params = new ArrayList<>();

        if (goods.getName() != null && !goods.getName().isEmpty()) {
            sql += " and g.`name` like '%" + goods.getName() + "%'";
        }
        if (goods.getTypeId() != null) {
            sql += " and g.type_id = " + goods.getTypeId();
        }
        if (goods.getFlag() != null) {
            sql += " and g.flag = " + goods.getFlag();
        }
        if (goods.getPubdate() != null){
            sql += "and pubdate = '" + DateUtils.format(goods.getPubdate()) + "'";
        }
        sql += " order by id desc";
        sql += " limit " + (pageNum - 1) * pageSize + "," + pageSize;
        System.out.println(sql);
        List<GoodsVo> goodsVo = JdbcUtilsV1.executeQuery(GoodsVo.class, sql);
        System.out.println(goodsVo);
        String countSql = "select count(*) from goods where deleted = 0";
        long count = JdbcUtilsV1.executeQueryOneValue(countSql);
        PageInfo<GoodsVo> pageInfo = new PageInfo<>(pageNum, pageSize, (int)count, goodsVo);
        return pageInfo;
    }
    // 添加
    public boolean insert(Goods goods) {
        String sql = "insert into goods(name,price,pubdate,type_id,intro,picture,flag,create_time) values(?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(goods.getName());
        params.add(goods.getPrice());
        params.add(goods.getPubdate());
        params.add(goods.getTypeId());
        params.add(goods.getIntro());
        params.add(goods.getPicture());
        params.add(goods.getFlag());
        params.add(goods.getCreateTime());
        int i = JdbcUtilsV1.executeUpdate(sql,params.toArray());
        return i > 0;
    }
// 删除
    public boolean delete(int id) {
        System.out.println(id);
        String sql = "update goods set deleted = 1 where id = ?";
        int i= JdbcUtilsV1.executeUpdate(sql, id);
        return i > 0;
    }
// 修改
    public boolean update(Goods goods) {
        String sql = "update goods set ";
        boolean isFirst = true; // 标志变量，用于判断是否是第一个字段

        if (goods.getName() != null && !goods.getName().isEmpty()) {
            sql += "name = '" + goods.getName() + "'";
            isFirst = false;
        }
        if (goods.getPrice() != null) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "price = " + goods.getPrice();
            isFirst = false;
        }
        if (goods.getPubdate() != null) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "pubdate = '" + DateUtils.format(goods.getPubdate()) + "'";
            isFirst = false;

        }
        if (goods.getTypeId() != null) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "type_id = " + goods.getTypeId();
            isFirst = false;
        }
        if (goods.getIntro() != null && !goods.getIntro().isEmpty()) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "intro = '" + goods.getIntro() + "'";
            isFirst = false;
        }
        if (goods.getPicture() != null && !goods.getPicture().isEmpty()) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "picture = '" + goods.getPicture() + "'";
            isFirst = false;
        }
        if (goods.getFlag() != null) {
            if (!isFirst) {
                sql += ", ";
            }
            sql += "flag = " + goods.getFlag();
            isFirst = false;
        }
        sql += " where id = " + goods.getId();
        int i = JdbcUtilsV1.executeUpdate(sql);
        return i > 0;
    }
}
